/**
 * Created by Shiun on 2018/5/9.
 */
$(function () {

    var address;

    $.ajax({
        url:'/address/queryAddress',
        type:'get',
        success: function (res) {
            console.log(res);
            address = res;
            var html = template('addressTpl',{result:res});
            //console.log(html);
            $('#address-box').html(html);
        }
    })

    //删除收货地址
    $('#address-box').on('click','.delete-btn', function () {
        var id = this.getAttribute('data-id');

        var li = this.parentNode.parentNode;

        console.log(li);
        mui.confirm("确认要删除吗？", function (message) {
            if(message.index == 1) {
                $.ajax({
                    url:' /address/deleteAddress',
                    type:'post',
                    data:{
                        id:id
                    },
                    success: function (res) {
                        if(res.success){
                            //重新加载页面
                            location.reload();
                        }
                    }
                })
            }else {
                //关闭列表滑出效果
                mui.swipeoutClose(li);
            }
        })
    })

    $('#address-box').on('tap','.edit-btn', function () {
        var id = this.getAttribute('data-id');

        for(var i=0;i<address.length;i++){
            if(address[i].id==id){

                localStorage.setItem('editAddress',JSON.stringify(address[i]));
                break;
            }
        }
        //跳转到编辑页面
        location.href = "addAddress.html";
        //console.log(address);
        //console.log(id);
    })
})